package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateShangbiao
//@description: 创建Shangbiao记录
//@param: shangbiao model.Shangbiao
//@return: err error

func CreateShangbiao(shangbiao model.Shangbiao) (err error) {
	err = global.GVA_DB.Create(&shangbiao).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteShangbiao
//@description: 删除Shangbiao记录
//@param: shangbiao model.Shangbiao
//@return: err error

func DeleteShangbiao(shangbiao model.Shangbiao) (err error) {
	err = global.GVA_DB.Delete(&shangbiao).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteShangbiaoByIds
//@description: 批量删除Shangbiao记录
//@param: ids request.IdsReq
//@return: err error

func DeleteShangbiaoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.Shangbiao{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateShangbiao
//@description: 更新Shangbiao记录
//@param: shangbiao *model.Shangbiao
//@return: err error

func UpdateShangbiao(shangbiao model.Shangbiao) (err error) {
	err = global.GVA_DB.Save(&shangbiao).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetShangbiao
//@description: 根据id获取Shangbiao记录
//@param: id uint
//@return: err error, shangbiao model.Shangbiao

func GetShangbiao(id uint) (err error, shangbiao model.Shangbiao) {
	err = global.GVA_DB.Where("id = ?", id).First(&shangbiao).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetShangbiaoInfoList
//@description: 分页获取Shangbiao记录
//@param: info request.ShangbiaoSearch
//@return: err error, list interface{}, total int64

func GetShangbiaoInfoList(info request.ShangbiaoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.Shangbiao{})
    var shangbiaos []model.Shangbiao
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Yaofang != "" {
        db = db.Where("`yaofang` = ?",info.Yaofang)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&shangbiaos).Error
	return err, shangbiaos, total
}